package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.WordStudyDTO;
import com.xuedongzhilian.service.IWordStudyService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 单词学习控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/study/word")
public class WordStudyController {

    @Resource
    private IWordStudyService wordStudyService;

    /**
     * 添加单词学习记录
     */
    @PostMapping
    public Result addWordStudy(@RequestBody WordStudyDTO wordStudyDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        wordStudyDTO.setUserId(userId);
        boolean success = wordStudyService.addWordStudy(wordStudyDTO);
        return success ? Result.ok() : Result.fail("添加单词学习记录失败");
    }

    /**
     * 获取用户的单词学习记录
     */
    @GetMapping
    public Result getWordStudies() {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        List<WordStudyDTO> wordStudies = wordStudyService.getWordStudiesByUserId(userId);
        return Result.ok(wordStudies);
    }

    /**
     * 获取单词学习详情
     */
    @GetMapping("/{wordId}")
    public Result getWordStudyDetail(@PathVariable("wordId") Long wordId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        WordStudyDTO wordStudyDTO = wordStudyService.getWordStudyDetail(wordId, userId);
        return wordStudyDTO != null ? Result.ok(wordStudyDTO) : Result.fail("单词学习记录不存在");
    }

    /**
     * 更新单词学习记录
     */
    @PutMapping
    public Result updateWordStudy(@RequestBody WordStudyDTO wordStudyDTO) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        wordStudyDTO.setUserId(userId);
        boolean success = wordStudyService.updateWordStudy(wordStudyDTO);
        return success ? Result.ok() : Result.fail("更新单词学习记录失败");
    }

    /**
     * 删除单词学习记录
     */
    @DeleteMapping("/{wordId}")
    public Result deleteWordStudy(@PathVariable("wordId") Long wordId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = wordStudyService.deleteWordStudy(wordId, userId);
        return success ? Result.ok() : Result.fail("删除单词学习记录失败");
    }

    /**
     * 标记单词为已掌握
     */
    @PostMapping("/{wordId}/master")
    public Result markWordAsMastered(@PathVariable("wordId") Long wordId) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        boolean success = wordStudyService.markWordAsMastered(wordId, userId);
        return success ? Result.ok() : Result.fail("标记单词为已掌握失败");
    }

    /**
     * 获取用户未掌握的单词
     */
    @GetMapping("/unmastered")
    public Result getUnmasteredWords(@RequestParam(defaultValue = "10") int count) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        List<WordStudyDTO> wordStudies = wordStudyService.getUnmasteredWords(userId, count);
        return Result.ok(wordStudies);
    }

    /**
     * 按难度等级获取用户未掌握的单词
     */
    @GetMapping("/unmastered/by-difficulty")
    public Result getUnmasteredWordsByDifficulty(
            @RequestParam(defaultValue = "10") int count,
            @RequestParam Integer difficulty) {
        // 获取当前登录用户ID
        Long userId = UserHolder.getUser().getId();
        List<WordStudyDTO> wordStudies = wordStudyService.getUnmasteredWordsByDifficulty(userId, count, difficulty);
        return Result.ok(wordStudies);
    }
}